package XSSDemo.dao.impl;

import XSSDemo.dao.MessageInfoDao;
import XSSDemo.pojo.MessageInfo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



public class MessageInfoDaoImpl implements MessageInfoDao {

    public boolean MessageInfoStoreDao(String name, String mail, String message) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean result = false;

        try {
            // 加载 MySQL 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql://192.168.124.3:3306/sec_xss", "root", "123456");

            // 准备 SQL 语句
            String sql = "INSERT INTO message (name, mail, message) VALUES (?, ?, ?)";
            ps = conn.prepareStatement(sql);

            // 设置参数
            ps.setString(1, name);
            ps.setString(2, mail);
            ps.setString(3, message);

            // 执行插入操作
            ps.execute();

            // 标记操作成功
            result = true;

        } catch (ClassNotFoundException e) {
            System.err.println("MySQL 驱动加载失败: " + e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("SQL 异常: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 确保资源被正确关闭
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    System.err.println("PreparedStatement 关闭失败: " + e.getMessage());
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    System.err.println("Connection 关闭失败: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }

        return result;
    }

    public List<MessageInfo> MessageInfoShowDao() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MessageInfo> messageinfo = new ArrayList<>();

        try {
            // 加载 MySQL 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql://192.168.124.3:3306/sec_xss", "root", "123456");

            // 准备 SQL 语句
            String sql = "SELECT * FROM message";
            ps = conn.prepareStatement(sql);

            // 执行查询操作
            rs = ps.executeQuery();

            // 处理结果集
            while (rs.next()) {
                MessageInfo msg = new MessageInfo();
                msg.setName(rs.getString("name"));
                msg.setMail(rs.getString("mail"));
                msg.setMessage(rs.getString("message"));
                messageinfo.add(msg);
            }

        } catch (ClassNotFoundException e) {
            System.err.println("MySQL 驱动加载失败: " + e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("SQL 异常: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 确保资源被正确关闭
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    System.err.println("ResultSet 关闭失败: " + e.getMessage());
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    System.err.println("PreparedStatement 关闭失败: " + e.getMessage());
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    System.err.println("Connection 关闭失败: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }

        return messageinfo;
    }
}